package userwashlogic

import (
	"context"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/userwash"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserWashListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserWashListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserWashListLogic {
	return &UserWashListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserWashListLogic) UserWashList(in *rpc.UserWashListRequest) (*rpc.UserWashListResponse, error) {
	if in.Page <= 0 {
		in.Page = 1
	}
	if in.PageSize <= 0 {
		in.PageSize = 20
	}
	resp := &rpc.UserWashListResponse{
		Meta: &rpc.Meta{
			Page:     in.Page,
			PageSize: in.PageSize,
		},
	}

	userWashes, total, err := userwash.NewUserWashModel(l.ctx, l.svcCtx.Db).ListUserWash(in.UserId, in.TypeName, in.Page, in.PageSize)
	if err != nil {
		return nil, err
	}
	resp.Meta.Total = total
	resp.List = make([]*rpc.UserWashData, 0, len(userWashes))
	for _, userWash := range userWashes {
		data := &rpc.UserWashData{
			Id:           userWash.ID,
			UserId:       userWash.UserId,
			TypeName:     userWash.TypeName,
			CategoryCode: userWash.CategoryCode,
			Rate:         userWash.Rate,
			Bet:          userWash.Bet,
			Value:        userWash.Value,
			CreatedAt:    userWash.CreatedAt,
		}
		resp.List = append(resp.List, data)
	}
	return resp, nil
}
